package com.model.bean;

import com.entites.EventLibrary;
import java.util.Date;
import java.util.ArrayList;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

@Stateless
public class EventLibraryFacade extends AbstractFacade<EventLibrary> implements EventLibraryFacadeLocal {

    @PersistenceContext(unitName = "BluePumpkinCompanyPU")
    private EntityManager em;

    @Override
    protected EntityManager getEntityManager() {
        return em;
    }

    public EventLibraryFacade() {
        super(EventLibrary.class);
    }

    @Override
    public List<EventLibrary> findAllEveventLibraryOrderByDesc(int begin_row, int end_row, String Username) {
        List<Object[]> obj;
        List<EventLibrary> list = new ArrayList<>();
        Query showAll = em.createNamedStoredProcedureQuery("EventLibrary.findAllEveventLibraryOrderByDesc");
        showAll.setParameter("beginRow", begin_row);
        showAll.setParameter("endRow", end_row);
        showAll.setParameter("Username", Username);
        obj = showAll.getResultList();
        for (Object[] row : obj) {
            int id = (int) row[0];
            String name = (String) row[1];
            String desc = (String) row[2];
            String evType = (String) row[3];
            Date timeStart = (Date) row[4];
            Date timeEnd = (Date) row[5];
            String image = (String) row[6];
            Date timeCreate = (Date) row[7];
            String status = (String) row[8];
            String type = (String) row[9];
            int commnetCount = ((int) row[10]);
            int joinedCount =((int) row[11]);
            EventLibrary ev = new EventLibrary(id, name, desc, evType, timeStart, timeEnd, image,timeCreate,status, type, commnetCount,joinedCount);
            list.add(ev);
        }
        return list;
    }

    @Override
    public EventLibrary findById(int id,String username) {
        List<Object[]> objArr;
        EventLibrary ev = new EventLibrary();

        Query query = em.createNamedStoredProcedureQuery("EventLibrary.getEventDetailsById");
              query.setParameter("eventId", id);
              query.setParameter("username", username);
        objArr = query.getResultList();
        for (Object[] row : objArr) {
            ev.setEventId((Integer) row[0]);
            ev.setName((String) row[1]);
            ev.setDescription((String) row[2]);
            ev.setEventType((String) row[3]);
            ev.setTimeStart((Date) row[4]);
            ev.setTimeEnd((Date) row[5]);
            ev.setImage((String) row[6]);
            ev.setTimeCreate((Date) row[7]);
            ev.setStatus((String) row[8]);
            ev.setType((String) row[9]);
            ev.setCommnetCount((int) row[10]);
            ev.setJoinedCount((int) row[11]);
        }
        return ev;

    }

    @Override
    public List<EventLibrary> findAllByEventType(String eventType) {
        List<Object[]> objArr;
        List<EventLibrary> list = new ArrayList<>();

        Query query = em.createNamedStoredProcedureQuery("EventLibrary.getListEventLibraryRelated");
        query.setParameter("eventType", eventType);
        objArr = query.getResultList();

        for (Object[] row : objArr) {
            EventLibrary ev = new EventLibrary();
            ev.setEventId((Integer) row[0]);
            ev.setName((String) row[1]);
            ev.setDescription((String) row[2]);
            ev.setEventType((String) row[3]);
            ev.setTimeStart((Date) row[4]);
            ev.setTimeEnd((Date) row[5]);
            ev.setCommnetCount((int) row[6]);

            list.add(ev);
        }
        return list;
    }

    @Override
    public List<EventLibrary> findEventLibraryByParameter(int beginrow, int endrow, String username, String parameter) {
        List<Object[]> obj;
        List<EventLibrary> list = new ArrayList<>();
        Query showAll = em.createNamedStoredProcedureQuery("EventLibrary.SearchEventLibrary");
        showAll.setParameter("beginRow", beginrow);
        showAll.setParameter("endRow", endrow);
        showAll.setParameter("username", username);
        showAll.setParameter("parameter", parameter);
        obj = showAll.getResultList();
        for (Object[] row : obj) {
            int id = (int) row[0];
            String name = (String) row[1];
            String desc = (String) row[2];
            String evType = (String) row[3];
            Date timeStart = (Date) row[4];
            Date timeEnd = (Date) row[5];
            String image = (String) row[6];
            Date timeCreate = (Date) row[7];
            String status = (String) row[8];
            String type = (String) row[9];
            int commnetCount = ((int) row[10]);
            int joinedCount =((int) row[11]);
            EventLibrary ev = new EventLibrary(id, name, desc, evType, timeStart, timeEnd,image,timeCreate, status, type,commnetCount,joinedCount);
            list.add(ev);
        }
        return list;
    }

    @Override
    public int countRecordSearch(String parameter) {
        Query query = em.createNamedStoredProcedureQuery("EventLibrary.getCountRecordSearch");
        query.setParameter("parameter", parameter);
        List<Object[]> obj = query.getResultList();
        int count = 0;
        for(Object[] row:obj){
            count=(int) row[0];
        }
        
        return count;
    }

    @Override
    public EventLibrary getUpcomingEvent(String username) {
        Query query = em.createNamedStoredProcedureQuery("EventLibrary.getEventUpcoming");
        query.setParameter("Username", username);
        List<Object[]> obj = query.getResultList();
       EventLibrary ev=null;
        for(Object[] row:obj){
           int id = (int) row[0];
            String name = (String) row[1];
            String desc = (String) row[2];
            String evType = (String) row[3];
            Date timeStart = (Date) row[4];
            Date timeEnd = (Date) row[5];
            String image = (String) row[6];
            Date timeCreate = (Date) row[7];
            String status = (String) row[8];
            String type = (String) row[10];
            int commnetCount = ((int) row[11]);
            int joinedCount =((int) row[12]);
            ev = new EventLibrary(id, name, desc, evType, timeStart, timeEnd,image,timeCreate, status, type,commnetCount,joinedCount); 
        }
        return ev;
    }

}
